package com.bdth.bdthiss.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.bdthiss.entity.Contract;
import com.bdth.bdthiss.entity.Payment;
import com.bdth.bdthiss.entity.Sendmk;
import com.bdth.bdthiss.entity.User;
import com.bdth.bdthiss.service.PaymentService;
import com.bdth.bdthiss.service.UserService;
import com.bdth.bdthiss.vo.PageVo;
import com.bdth.bdthiss.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 收款记录 前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-08-27
 */
@Controller
@RequestMapping("/payment")
public class PaymentController extends BaseController {
    @Autowired
    private PaymentService paymentService;

    @Autowired
    private UserService userService;
    @RequestMapping(value = "index")
    public String index() {
        return "payment/index";
    }

    @RequestMapping(value = "search")
    public String search() {
        return "payment/search";
    }

    @RequestMapping(value = "form")
    public String form() {
        return "payment/form";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Payment> getPageList(HttpServletRequest request, Pagination<Contract> pagination,
                                       @RequestParam(required = false) String code,
                                       @RequestParam(required = false) String name,
                                       @RequestParam(required = false) String start,
                                       @RequestParam(required = false) String end){
        Map<String ,Object> query = new HashMap<>();
        //查询条件
        if(name!=null && !name.equals(""))
            query.put("name",name);
        if(code!=null && !code.equals(""))
            query.put("code",code);
        if(start!=null && !start.equals(""))
            query.put("start",start);
        if(end!=null && !end.equals(""))
            query.put("end",end);
        User session = (User)request.getSession().getAttribute("loginUser");
        if(session.getGroupid()!=0){
            String groups = session.getGroups();
            if(groups.equals("财务人员")){
                //只能看到自己的合同
                query.put("opid",session.getId());
            }
            if(groups.equals("审核人员")){
                Integer departid = session.getDepartid();
                EntityWrapper<User> userEntityWrapper = new EntityWrapper<>();
                userEntityWrapper.eq("departid",departid);
                List<User> ls = userService.selectList(userEntityWrapper);
                String sign="";
                for(User u:ls){
                    sign+= u.getId()+",";
                }
                sign += "0";
                query.put("opid",sign);
            }
        }
        Page<Payment> items = paymentService.listPages(pagination, query);
        PageVo<Payment> pageVo = new PageVo<>(items.getRecords(),items.getTotal());
        return pageVo;
    }
}

